home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / aiPlayer.cs next >
Text File  |  2005-11-23  |  7KB  |  243 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------------------
  7. // AIPlayer callbacks
  8. // The AIPlayer class implements the following callbacks:
  9. //
  10. //    PlayerData::onStuck(%this,%obj)
  11. //    PlayerData::onUnStuck(%this,%obj)
  12. //    PlayerData::onStop(%this,%obj)
  13. //    PlayerData::onMove(%this,%obj)
  14. //    PlayerData::onReachDestination(%this,%obj)
  15. //    PlayerData::onTargetEnterLOS(%this,%obj)
  16. //    PlayerData::onTargetExitLOS(%this,%obj)
  17. //    PlayerData::onAdd(%this,%obj)
  18. //
  19. // Since the AIPlayer doesn't implement it's own datablock, these callbacks
  20. // all take place in the PlayerData namespace.
  21. //-----------------------------------------------------------------------------
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Demo Pathed AIPlayer.
  25. //-----------------------------------------------------------------------------
  26.  
  27. datablock PlayerData(DemoPlayer : PlayerBody)
  28. {
  29.    className = "PathedPlayer";
  30.    maxInv[CrossbowAmmo] = 500000;  // Lots of ammo for the demo
  31.    demo = true;
  32. };
  33.  
  34. function DemoPlayer::onReachDestination(%this,%obj)
  35. {
  36.    // Moves to the next node on the path.
  37.    // Override for all player. Normally we'd override this for only
  38.    // a specific player datablock or class of players.
  39.    if (%obj.path !$= "") {
  40.       if (%obj.currentNode == %obj.targetNode)
  41.          %this.onEndOfPath(%obj,%obj.path);
  42.       else
  43.          %obj.moveToNextNode();
  44.    }
  45. }
  46.  
  47. function DemoPlayer::onEndOfPath(%this,%obj,%path)
  48. {
  49.    %obj.nextTask();
  50. }
  51.  
  52. function DemoPlayer::onEndSequence(%this,%obj,%slot)
  53. {
  54.    echo("Sequence Done!");
  55.    %obj.stopThread(%slot);
  56.    %obj.nextTask();
  57. }
  58.  
  59.  
  60. //-----------------------------------------------------------------------------
  61. // AIPlayer static functions
  62. //-----------------------------------------------------------------------------
  63.  
  64. function AIPlayer::spawn(%name,%spawnPoint)
  65. {
  66.    // Create the demo player object
  67.    %player = new AiPlayer() {
  68.       dataBlock = DemoPlayer;
  69.       path = "";
  70.    };
  71.    MissionCleanup.add(%player);
  72.    %player.setShapeName(%name);
  73.    %player.setTransform(%spawnPoint);
  74.    return %player;
  75. }
  76.  
  77. function AIPlayer::spawnOnPath(%name,%path)
  78. {
  79.    // Spawn a player and place him on the first node of the path
  80.    if (!isObject(%path))
  81.       return;
  82.    %node = %path.getObject(0);
  83.    %player = AIPlayer::spawn(%name,%node.getTransform());
  84.    return %player;
  85. }
  86.  
  87.  
  88. //-----------------------------------------------------------------------------
  89. // AIPlayer methods 
  90. //-----------------------------------------------------------------------------
  91.  
  92. function AIPlayer::followPath(%this,%path,%node)
  93. {
  94.    // Start the player following a path
  95.    %this.stopThread(0);
  96.    if (!isObject(%path)) {
  97.       %this.path = "";
  98.       return;
  99.    }
  100.    if (%node > %path.getCount() - 1)
  101.       %this.targetNode = %path.getCount() - 1;
  102.    else
  103.       %this.targetNode = %node;
  104.    if (%this.path $= %path)
  105.       %this.moveToNode(%this.currentNode);
  106.    else {
  107.       %this.path = %path;
  108.       %this.moveToNode(0);
  109.    }
  110. }
  111.  
  112. function AIPlayer::moveToNextNode(%this)
  113. {
  114.    if (%this.targetNode < 0 || %this.currentNode < %this.targetNode) {
  115.       if (%this.currentNode < %this.path.getCount() - 1)
  116.          %this.moveToNode(%this.currentNode + 1);
  117.       else
  118.          %this.moveToNode(0);
  119.    }
  120.    else
  121.       if (%this.currentNode == 0)
  122.          %this.moveToNode(%this.path.getCount() - 1);
  123.       else
  124.          %this.moveToNode(%this.currentNode - 1);
  125. }
  126.  
  127. function AIPlayer::moveToNode(%this,%index)
  128. {
  129.    // Move to the given path node index
  130.    %this.currentNode = %index;
  131.    %node = %this.path.getObject(%index);
  132.    %this.setMoveDestination(%node.getTransform(), %index == %this.targetNode);
  133. }
  134.  
  135.  
  136. //-----------------------------------------------------------------------------
  137. //
  138. //-----------------------------------------------------------------------------
  139.  
  140. function AIPlayer::pushTask(%this,%method)
  141. {
  142.    if (%this.taskIndex $= "") {
  143.       %this.taskIndex = 0;
  144.       %this.taskCurrent = -1;
  145.    }
  146.    %this.task[%this.taskIndex] = %method; 
  147.    %this.taskIndex++;
  148.    if (%this.taskCurrent == -1)
  149.       %this.executeTask(%this.taskIndex - 1);
  150. }
  151.  
  152. function AIPlayer::clearTasks(%this)
  153. {
  154.    %this.taskIndex = 0;
  155.    %this.taskCurrent = -1;
  156. }
  157.  
  158. function AIPlayer::nextTask(%this)
  159. {
  160.    if (%this.taskCurrent != -1)
  161.       if (%this.taskCurrent < %this.taskIndex - 1)
  162.          %this.executeTask(%this.taskCurrent++);
  163.       else
  164.          %this.taskCurrent = -1;
  165. }
  166.  
  167. function AIPlayer::executeTask(%this,%index)
  168. {
  169.    %this.taskCurrent = %index;
  170.    eval(%this.getId() @ "." @ %this.task[%index] @ ";");
  171. }
  172.  
  173.  
  174. //-----------------------------------------------------------------------------
  175.  
  176. function AIPlayer::singleShot(%this)
  177. {
  178.    // This shooting delay is here for the demo, don't want to fire
  179.    // at the weapon's full rate.
  180.    %this.setImageTrigger(0,true);
  181.    %this.setImageTrigger(0,false);
  182.    %this.trigger = %this.schedule(2000,singleShot);
  183. }
  184.  
  185. //-----------------------------------------------------------------------------
  186.  
  187. function AIPlayer::wait(%this,%time)
  188. {
  189.    %this.schedule(%time * 1000,"nextTask");
  190. }
  191.  
  192. function AIPlayer::done(%this,%time)
  193. {
  194.    %this.schedule(0,"delete");
  195. }
  196.  
  197. function AIPlayer::fire(%this,%bool)
  198. {
  199.    if (%bool) {
  200.       cancel(%this.trigger);
  201.       %this.singleShot();
  202.    }
  203.    else
  204.       cancel(%this.trigger);
  205.    %this.nextTask();
  206. }
  207.  
  208. function AIPlayer::aimAt(%this,%object)
  209. {
  210.    echo("Aim: " @ %object);
  211.    %this.setAimObject(%object);
  212.    %this.nextTask();
  213. }
  214.  
  215. function AIPlayer::animate(%this,%seq)
  216. {
  217.    //%this.stopThread(0);
  218.    //%this.playThread(0,%seq);
  219.    %this.setActionThread(%seq);
  220. }
  221.  
  222. function AIPlayer::test()
  223. {
  224.    %player = AIPlayer::spawnOnPath("xasd","MissionGroup/Paths/Path2");
  225.    %player.mountImage(CrossbowImage,0);
  226.    %player.setInventory(CrossbowAmmo,1000);
  227.    
  228.    %player.pushTask("followPath(\"MissionGroup/Paths/Path2\")");
  229.    %player.pushTask("aimAt(\"MissionGroup/Room6/target\")");
  230.    %player.pushTask("wait(1)");   
  231.    %player.pushTask("fire(true)");
  232.    %player.pushTask("wait(10)");
  233.    %player.pushTask("fire(false)");
  234.    %player.pushTask("playThread(0,\"celwave\")");
  235.    %player.pushTask("done()");
  236. }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.